//定义根目录路径
var baseUrl = "app/";
// 定义css目录路径
var cssUrl = "app/public/css/";
// 定义sass目录路径
var sassUrl = "app/public/scss/";
// 定义js目录路径
var jsUrl = "app/js/";

var gulp = require('gulp'),
    // sass编译
    sass = require('gulp-sass'),
    // css压缩
    minifycss = require('gulp-clean-css'),
    // js压缩
    uglify = require('gulp-uglify'),
    // 清空文件夹
    clean = require('gulp-clean'),
    // 控制task顺序
    gulpSequence = require('gulp-sequence');

//sass编译
gulp.task('sass', function() {
    return gulp.src(sassUrl + '**/*.scss')
        .pipe(sass({
            outputStyle: 'compressed'
        }))
        .pipe(gulp.dest(cssUrl))
});

//css压缩
gulp.task('cssMin', function() {
    return gulp.src(cssUrl + '**/*.css')
        .pipe(minifycss())
        .pipe(gulp.dest(distCssUrl));
});

//js压缩
gulp.task('jsMin', function() {
    return gulp.src(jsUrl + '**/*.js')
        .pipe(uglify().on('error', function(e) {
            console.log('我是uglify的错误', e);
        }))
        .pipe(gulp.dest(distJsUrl));
});

// 清空输出目录并删除改目录
gulp.task('clean', function() {
    return gulp.src([distCssUrl, distJsUrl], { read: false })
        .pipe(clean({ force: true }));
});

//监控
gulp.task('watch', function() {
    gulp.watch(sassUrl + '**/*.scss', ['sass']);
});

// 打包任务
// []中的是异步，运行完后运行rev
gulp.task("build", function(cb) {
    gulpSequence(['clean'], ['cssMin', 'jsMin'])(cb);
});
